<?php

class StockController extends BaseController
{
	public $leftmenusTitle = '常用操作';
	
	
	protected function beforeAction($action)
    {
    	parent::beforeAction();
    	$this->leftmenus = array();
    	# code...
    	$this->leftmenus[] = array('href'=>$this->createUrl('stock/index'),'menutext'=>'进销存报表','isValid'=>true);
    	$this->leftmenus[] = array('href'=>$this->createUrl('stock/balance'),'menutext'=>'销售差异比对','isValid'=>true);
    	Yii::app()->clientScript->registerCoreScript('jquery');
        return true;
    } 
    
	/**
	 * 进销存报表
	 */
	public function actionIndex()
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/depart'));
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/producttypeProduct'));
		$this->title='进销存报表';
		$model=new StockSearchForm;
		$criteria=new CDbCriteria(array(  
			   //'defaultorder'=>'trade_date',  
			   'with'=>array('eparchyCode','departCode','product',),
			)); 

		if(isset($_GET['StockSearchForm']))
		{
			$model->attributes=$_GET['StockSearchForm'];
		}
		else
		{
			$model->attributes=array(
				'eparchy_code'=>Yii::app()->session['currenUser']->eparchy_code,
			    'depart_code'=>Yii::app()->session['currenUser']->depart_code,
			    'start_date'=>date('Y-m-d'),
			    'end_date'=>date('Y-m-d'),
			);
		}
		
		if(!isset($_GET['export']))
		{
			$model->saveCondition();
		}
		
		if($model->validate())
		{
			$criteria->addSearchCondition('t.eparchy_code',isset($_POST['export'])?$model->old_eparchy_code:$model->eparchy_code);
			$criteria->addSearchCondition('t.depart_code',isset($_POST['export'])?$model->old_depart_code:$model->depart_code);
			$criteria->compare('t.calc_day','>='.(isset($_POST['export'])?$model->old_start_date:$model->start_date));
			$criteria->compare('t.calc_day','<='.(isset($_POST['export'])?$model->old_end_date:$model->end_date));
			$criteria->addSearchCondition('product.producttype_code',isset($_POST['export'])?$model->old_producttype_code:$model->producttype_code);
			$criteria->addSearchCondition('t.product_id',isset($_POST['export'])?$model->old_product_id:$model->product_id);
		}
		//通过session的方式求取库存周转率
		Yii::app()->session['search_dura']=(strtotime($model->end_date)-strtotime($model->start_date))/24/60/60+1;
		$dataProvider=new CActiveDataProvider('Dayreport', array(
			    'criteria'=>$criteria,
			    'pagination'=>array(
			        'pageSize'=>Yii::app()->params['reportsPerPage'],
			    ),
			));
		if(isset($_GET['export']))
		{
			$this->renderPartial('indexexport',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
		else
		{
			//Yii::log(CJSON::encode($criteria),'info');
			$this->render('index',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
		
	}
	
	//销售差异比对报表
	public function actionBalance()
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/depart'));
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/producttypeProduct'));
		$this->title='销售差异比对 ';
		$model=new StockSearchForm;
		$criteria=new CDbCriteria(array(  
			   'with'=>array('eparchyCode','departCode','product',),
			)); 

		if(isset($_GET['StockSearchForm']))
		{
			$model->attributes=$_GET['StockSearchForm'];
		}
		else
		{
			$model->attributes=array(
				'eparchy_code'=>Yii::app()->session['currenUser']->eparchy_code,
			    'depart_code'=>Yii::app()->session['currenUser']->depart_code,
			    'start_date'=>date('Y-m-d'),
			    'end_date'=>date('Y-m-d'),
			);
		}
		
		if(!isset($_GET['export']))
		{
			$model->saveCondition();
		}
		
		if($model->validate())
		{
			$criteria->addSearchCondition('t.eparchy_code',isset($_POST['export'])?$model->old_eparchy_code:$model->eparchy_code);
			$criteria->addSearchCondition('t.depart_code',isset($_POST['export'])?$model->old_depart_code:$model->depart_code);
			$criteria->compare('t.calc_day','>='.(isset($_POST['export'])?$model->old_start_date:$model->start_date));
			$criteria->compare('t.calc_day','<='.(isset($_POST['export'])?$model->old_end_date:$model->end_date));
			$criteria->addSearchCondition('product.producttype_code',isset($_POST['export'])?$model->old_producttype_code:$model->producttype_code);
			$criteria->addSearchCondition('t.product_id',isset($_POST['export'])?$model->old_product_id:$model->product_id);
		}
		$dataProvider=new CActiveDataProvider('Dayreport', array(
			    'criteria'=>$criteria,
			    'pagination'=>array(
			        'pageSize'=>Yii::app()->params['reportsPerPage'],
			    ),
			));
		if(isset($_GET['export']))
		{
			$this->renderPartial('balanceexport',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
		else
		{
			$this->render('balance',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
	}

}